package com.xudow.app.ui.task;

import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;

import com.activeshare.edu.ucenter.common.messages.course.CourseDetailMessage;
import com.activeshare.edu.ucenter.common.messages.course.ListCourseMessage;
import com.activeshare.edu.ucenter.models.course.CourseWithOtherInfo;
import com.xudow.app.Config;
import com.xudow.app.XApplication;
import com.xudow.app.util.FileUtils;
import com.xudow.app.util.StringUtils;
import com.xudow.app.util.rest.XRestTemplate;

import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.web.client.RestTemplate;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;

import static com.xudow.app.util.LogUtils.LOGE;
import static com.xudow.app.util.LogUtils.makeLogTag;

/**
 * 获取课程列表Task
 *
 * @author lukecheng
 */
public class CourseDetailTask extends AsyncTask<HashMap<String, String>, Void, Void> {
    private static final String TAG = makeLogTag(CourseDetailTask.class);

    private static final String COURSE_DETAIL_URL = Config.BASE_URL + Config.COURSE_DETAIL_URL;

    public static final int SUCCESS = 0;
    public static final int ERROR = 1;

    private Context context;
    private Handler handler;

    public CourseDetailTask(Context context, Handler handler) {
        this.context = context;
        this.handler = handler;
    }

    private void sendErrorMessage(String msg) {
        android.os.Message message = new android.os.Message();
        message.what = ERROR;

        if (null != msg) {
            Bundle data = new Bundle();
            data.putString("errMessage", msg);
            message.setData(data);
        }

        handler.sendMessage(message);
    }

    private void sendSuccessMessage(CourseDetailMessage result) {
        android.os.Message message = new android.os.Message();
        message.what = SUCCESS;

        Bundle data = new Bundle();
        data.putSerializable("course", result.getCourse());
        data.putString("schoolImage", result.getSchoolImage());
        data.putInt("schoolImageCount", result.getSchoolImageCount());
        message.setData(data);

        handler.sendMessage(message);
    }

    @Override
    protected Void doInBackground(HashMap<String, String>... parameters) {
        XApplication application = (XApplication) context.getApplicationContext();

        XRestTemplate restTemplate = new XRestTemplate(application.getCookieStore(), context);
       // XRestTemplate restTemplate1 = new XRestTemplate(application.getCookieStore(), context);
        restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
       //restTemplate1.getMessageConverters().add(new StringHttpMessageConverter());
        LinkedMultiValueMap<String, String> valueMap = new LinkedMultiValueMap<String, String>();

        HashMap<String, String> params = parameters[0];
        valueMap.add("courseid", params.get("courseid"));
           Log.i("CourseDetailTask","params.get(\"courseid\")>>"+params.get("courseid"));
        try {
            CourseDetailMessage result = restTemplate.postForObject(COURSE_DETAIL_URL, valueMap,
                    CourseDetailMessage.class);
            Log.i("CourseDetailTask","COURSE_DETAIL_URL>>"+COURSE_DETAIL_URL);
            /*String result1 = restTemplate.postForObject(COURSE_DETAIL_URL, valueMap,
                    String.class);*/
            if (result != null) {
               //FileUtils.writeFile(result1,new File(context.getCacheDir().getAbsolutePath(),"coursedetail.txt"));
                if (result.getResult().equals("0")) {
                    sendSuccessMessage(result);
                    Log.i("CourseDetailTask", "result>" + result);
                } else if (result.getResult().equals("1")) {
                    sendErrorMessage(result.getErrorMessage());
                    Log.i("CourseDetailTask", "result.getErrorMessage()>"+result.getErrorMessage());
                }
            } else {
                sendErrorMessage(null);
                Log.i("CourseDetailTask", "null" );
            }
        } catch (Exception e) {
            LOGE(TAG, e.getMessage());
            Log.i("CourseDetailTask","e.getMessage()"+e.getMessage());
            sendErrorMessage(e.getMessage());
        }

        return null;
    }
}
